from django.test.client import Client
from django.test import TestCase
from django.shortcuts import render_to_response

from inv.admin.models  import *
from inv.entity.models import *
from inv.utils.errors  import *

class ComboContentTest(TestCase):
    def setUp(self):
        # Every test needs a client.
        self.client = Client()

    def test_empty_combo_content_name(self):
        cls1 = Class(nam='Class1')
        cls1.save()
        grp1 = Class_Group(nam='Grp1', cls_id=cls1.id)
        grp1.save()
        met1 = Class_Meta(nam='Meta1', typ="COMBO", grp_id=grp1.id, cls_id=cls1.id)
        met1.save()
        response = self.client.post('/entity/combo/' + str(cls1.id) + '/' + str(met1.id) + '/',
         {'COMBO_CONTENT': ''})
        expected = render_to_response('ajax_response.json', err[4][1])
        self.assertEqual(response._container, expected._container)

    def test_already_exists(self):
        
        cls1 = Class(nam='Class1')
        cls1.save()
        grp1 = Class_Group(nam='Grp1', cls_id=cls1.id)
        grp1.save()
        met1 = Class_Meta(nam='Meta1', typ="COMBO", grp_id=grp1.id, cls_id=cls1.id)
        met1.save()
        response = self.client.post('/entity/combo/' + str(cls1.id) + '/' + str(met1.id) + '/',
         {'COMBO_CONTENT': 'Content1'})
        response = self.client.post('/entity/combo/' + str(cls1.id) + '/' + str(met1.id) + '/',
         {'COMBO_CONTENT': 'Content1'})
        expected = render_to_response('ajax_response.json', err[4][2])
        self.assertEqual(response._container, expected._container)

    def test_success(self):
        cls1 = Class(nam='Class1')
        cls1.save()
        grp1 = Class_Group(nam='Grp1', cls_id=cls1.id)
        grp1.save()
        met1 = Class_Meta(nam='Meta1', typ="COMBO", grp_id=grp1.id, cls_id=cls1.id)
        met1.save()
        response = self.client.post('/entity/combo/' + str(cls1.id) + '/' + str(met1.id) + '/',
         {'COMBO_CONTENT': 'Content1'})
        expected = render_to_response('ajax_response.json', err[4][0])
        self.assertEqual(response._container, expected._container)
        
    def test_verify_data_refining(self):
        cls1 = Class(nam='Class1')
        cls1.save()
        grp1 = Class_Group(nam='Grp1', cls_id=cls1.id)
        grp1.save()
        met1 = Class_Meta(nam='Meta1', typ="COMBO", grp_id=grp1.id, cls_id=cls1.id)
        met1.save()
        response = self.client.post('/entity/combo/' + str(cls1.id) + '/' + str(met1.id) + '/',
         {'COMBO_CONTENT': 'Content   1'})
        response = self.client.post('/entity/combo/' + str(cls1.id) + '/' + str(met1.id) + '/',
         {'COMBO_CONTENT': 'ContENt       1'})
        expected = render_to_response('ajax_response.json', err[4][2])
        self.assertEqual(response._container, expected._container)
      
    def test_invalid_class_id(self):
        cls1 = Class(nam='Class1')
        cls1.save()
        grp1 = Class_Group(nam='Grp1', cls_id=cls1.id)
        grp1.save()
        met1 = Class_Meta(nam='Meta1', typ="COMBO", grp_id=grp1.id, cls_id=cls1.id)
        met1.save()
        response = self.client.post('/entity/combo/' + str(cls1.id + 1) + '/' + str(met1.id) + '/',
         {'COMBO_CONTENT': 'Content1'})
        expected = render_to_response('ajax_response.json', err[4][3])
        self.assertEqual(response._container, expected._container)
     
    def test_invalid_meta_id(self):
        cls1 = Class(nam='Class1')
        cls1.save()
        grp1 = Class_Group(nam='Grp1', cls_id=cls1.id)
        grp1.save()
        met1 = Class_Meta(nam='Meta1', typ="COMBO", grp_id=grp1.id, cls_id=cls1.id)
        met1.save()
        response = self.client.post('/entity/combo/' + str(cls1.id) + '/' + str(met1.id + 1) + '/',
         {'COMBO_CONTENT': 'Content1'})
        expected = render_to_response('ajax_response.json', err[4][4])
        self.assertEqual(response._container, expected._container)

    def test_invalid_meta_type(self):
        cls1 = Class(nam='Class1')
        cls1.save()
        grp1 = Class_Group(nam='Grp1', cls_id=cls1.id)
        grp1.save()
        met1 = Class_Meta(nam='Meta1', typ="CHAR", grp_id=grp1.id, cls_id=cls1.id)
        met1.save()
        response = self.client.post('/entity/combo/' + str(cls1.id) + '/' + str(met1.id) + '/',
         {'COMBO_CONTENT': 'Content1'})
        expected = render_to_response('ajax_response.json', err[4][5])
        self.assertEqual(response._container, expected._container)

    def test_same_meta_diff_class(self):
        cls1 = Class(nam='Class1')
        cls1.save()
        grp1 = Class_Group(nam='Grp1', cls_id=cls1.id)
        grp1.save()
        met1 = Class_Meta(nam='Meta1', typ="COMBO", grp_id=grp1.id, cls_id=cls1.id)
        met1.save()
        cls2 = Class(nam='Class2')
        cls2.save()
        grp2 = Class_Group(nam='Grp1', cls_id=cls2.id)
        grp2.save()
        met2 = Class_Meta(nam='Meta1', typ="COMBO", grp_id=grp2.id, cls_id=cls2.id)
        met2.save()
        response = self.client.post('/entity/combo/' + str(cls1.id) + '/' + str(met1.id) + '/',
         {'COMBO_CONTENT': 'Content1'})
        expected = render_to_response('ajax_response.json', err[4][0])
        self.assertEqual(response._container, expected._container)
        response = self.client.post('/entity/combo/' + str(cls2.id) + '/' + str(met2.id) + '/',
         {'COMBO_CONTENT': 'Content1'})
        expected = render_to_response('ajax_response.json', err[4][0])
        self.assertEqual(response._container, expected._container)

    def test_diff_meta_same_class(self):
        cls1 = Class(nam='Class1')
        cls1.save()
        grp1 = Class_Group(nam='Grp1', cls_id=cls1.id)
        grp1.save()
        met1 = Class_Meta(nam='Meta1', typ="COMBO", grp_id=grp1.id, cls_id=cls1.id)
        met1.save()
        met2 = Class_Meta(nam='Meta2', typ="COMBO", grp_id=grp1.id, cls_id=cls1.id)
        met2.save()
        response = self.client.post('/entity/combo/' + str(cls1.id) + '/' + str(met1.id) + '/',
         {'COMBO_CONTENT': 'Content1'})
        expected = render_to_response('ajax_response.json', err[4][0])
        self.assertEqual(response._container, expected._container)
        response = self.client.post('/entity/combo/' + str(cls1.id) + '/' + str(met2.id) + '/',
         {'COMBO_CONTENT': 'Content1'})
        expected = render_to_response('ajax_response.json', err[4][0])
        self.assertEqual(response._container, expected._container)
